package cmich.cps.scheduler.gui;

import java.util.logging.Logger;

import javax.swing.JOptionPane;

/**
 * The Class Prompts.
 *
 * @author Jonathan Kissinger <kissi1j@cmich.edu>
 */
public class Prompts {
	
	/** The Constant LOGGER. */
	private final static Logger LOGGER = Logger.getLogger(Prompts.class.getName());
	
	/** The Constant instance. */
	private final static Prompts instance = new Prompts();
	
	/** The frame. */
	private Frame frame;
	
	/**
	 * Instantiates a new prompts.
	 */
	private Prompts() {}
	
	/**
	 * Gets the single instance of Prompts.
	 *
	 * @return single instance of Prompts
	 */
	public static Prompts getInstance() {
		return instance;
	}
	
	/**
	 * Initializes the variables.
	 */
	public void init() {
		frame = Frame.getInstance();
	}
	
	/**
	 * Fatal error message.
	 *
	 * @param msg the message
	 */
	public void fatalError(String msg) {
		int width = 300;
		msg = "<html><body width='" + width + "'><h1>Fatal Error</h1><p>" + msg;
		msg = msg + "<br><br>Click ok to close the scheduler</p></body></html>";
		JOptionPane.showMessageDialog(frame, msg, "Fatal error", JOptionPane.ERROR_MESSAGE);
		System.exit(1);
	}

	/**
	 * Non fatal error message.
	 *
	 * @param msg the message
	 */
	public void nonFatalError(String msg) {
		int width = 300;
		msg = "<html><body width='" + width + "'><h1>Error</h1><p>" + msg;
		msg = msg + "<br><br>Click ok to continue, cancel to close the scheduler</p></body></html>";
		int result = JOptionPane.showConfirmDialog(frame, msg, "Error", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
		if(result==JOptionPane.CANCEL_OPTION) {
			LOGGER.info("User chose to close the program.");
			System.exit(0);
		}
	}
	
    /**
     * Confirmation message.
     *
     * @param msg the message
     * @return true, if successful
     */
    public boolean confirm(String msg) {
        int result = JOptionPane.showConfirmDialog(frame, msg, "Confirmation needed", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
        if(result==JOptionPane.YES_OPTION) {
                return true;
        }
        return false;
}

	/**
	 * Gets the input.
	 *
	 * @param msg the message
	 * @param initialValue the string
	 * @return the input
	 */
	public String getInput(String msg, String initialValue) {
        return JOptionPane.showInputDialog(frame, msg, initialValue);
	}
	
	/**
	 * Message.
	 *
	 * @param msg the message
	 */
	public void message(String msg) {
        JOptionPane.showMessageDialog(frame, msg);
	}

	/**
	 * Gets the category.
	 *
	 * @return the category
	 */
	public String getCategory() {
		Object[] options = { "Course", "Room", "Timeslot" };
		int n = JOptionPane.showOptionDialog(frame, "What would you like to add a preference for?",
			    "Preference Category",
			    JOptionPane.YES_NO_CANCEL_OPTION,
			    JOptionPane.QUESTION_MESSAGE,
			    null,
			    options,
			    options[0]);
		if(n==JOptionPane.YES_OPTION) {
			return "course";
		}
		if(n==JOptionPane.NO_OPTION) {
			return "room";
		}
		if(n==JOptionPane.CANCEL_OPTION) {
			return "timeslot";
		}
		return null;
	}

	/**
	 * Gets the item.
	 *
	 * @param choices the choices
	 * @return the item
	 */
	public String getItem(Object[] choices) {
		String item = (String) JOptionPane.showInputDialog(frame, "Choose one:",
                "Choose item",
                JOptionPane.PLAIN_MESSAGE,
                null,
                choices,
                choices[0]);
		return item;
	}
}